/**
 * 
 */
package com.td.framework.webmanager.qxygns.service.impl;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.td.framework.webmanager.domain.AppsysQxygns;
import com.td.framework.webmanager.domain.AppsysYmgnBean;
import com.td.framework.webmanager.qxygns.dao.QxygnsDao;
import com.td.framework.webmanager.qxygns.service.QxygnsService;
import com.td.framework.webmanager.ymgn.dao.YmgnDao;

/**
 * 
 * Title:权限与功能树业务层实现类 Description: 处理权限与功能树信息 Copyright: Copyright (c) 2008
 * Company 北京水软科技有限公司
 * 
 * @author FLT006
 * @version 1.0
 */
public class QxygnsServiceImpl implements QxygnsService {

	private QxygnsDao qxygnsDao = null;// 权限与功能树Dao;
	private YmgnDao ymgnDao = null;// 页面功能Dao;
	private String sql = "";

	/**
	 * @toDo:删除权限与功能树信息,支持批量删除;
	 * @param ymbhs
	 * @return boolean;
	 */
	public boolean deleteAppsysQxygns(String[] ymbhs) {
		// TODO Auto-generated method stub
		return qxygnsDao.deleteAppsysQxygns(ymbhs);
	}

	/**
	 * @toDo:得到总记录数;
	 * @return rows;
	 */
	public int getRows() {
		return qxygnsDao.getRows();
	}

	/**
	 * @toDo:通过上级页面编号查询权限与功能树信息;
	 * @param pageSize,startRow
	 * @return qxygnsList;
	 */
	@SuppressWarnings("unchecked")
	public List findBySjbh(int pageSize, int startRow) {
		// TODO Auto-generated method stub
		List<AppsysQxygns> list = qxygnsDao.findBySjbh(pageSize, startRow);
		return list;
	}

	/**
	 * @toDo:通过页面编号查询权限与功能树对象;
	 * @param ymbh
	 * @return qxygns;
	 */
	public AppsysQxygns getAppsysQxygns(String ymbh) {
		// TODO Auto-generated method stub
		return qxygnsDao.findByYmbh(ymbh);
	}

	/**
	 * @toDo 生成系统树;
	 * @return list;
	 */
	@SuppressWarnings("unchecked")
	public List<AppsysQxygns> getTreeList() {
		// TODO Auto-generated method stub
		return qxygnsDao.getTreeList();
	}

	/**
	 * @toDo:用户登录成功左边菜单树;
	 * @param yhbh
	 * @return list
	 */
	@SuppressWarnings("unchecked")
	public List getMainTreeList(String yhbh,String sjbh,String isMenu) {
		boolean flag = qxygnsDao.checkYhlx(yhbh);
		String yhlx = "0";//用户类型
		if(flag==true)
		{
			yhlx = "1";
		}
		List list = new ArrayList();
		list.add(yhlx);
		list.add(qxygnsDao.getMainTreeList(yhbh,sjbh,flag));
		return list;
	}
	/**
	 * @toDo:用户登录成功左边菜单树;
	 * @param yhbh
	 * @return list
	 */
	@SuppressWarnings("unchecked")
	public List getMainTreeList(String yhbh,String sjbh) {
		return qxygnsDao.getMainTreeList(yhbh,sjbh,false);
	}
	/**
	 *得到首页的主要大功能菜单 
	 *@return 首页大功能
	 */
	public List getMainFunc(String yhbh){
		return qxygnsDao.getMainFunc(yhbh);
	}
	public List getMainTreeList(String yhbh){
		return qxygnsDao.getMainTreeList(yhbh);
	}
	/**
	 * @toDo:保存权限与功能树信息;
	 * @param appsysQxygns
	 * @param gnbhs 
	 * @return boolean;
	 */
	public boolean addAppsysQxygns(AppsysQxygns appsysQxygns, String[] gnbhs) {
		// TODO Auto-generated method stub
		return qxygnsDao.addAppsysQxygns(appsysQxygns,gnbhs);
	}

	/**
	 * @toDo:更新权限与功能树信息;
	 * @param appsysQxygns
	 * @param gnbhs 
	 * @return boolean;
	 */
	public boolean updateAppsysQxygns(AppsysQxygns appsysQxygns,String[] gnbhs) {
		// TODO Auto-generated method stub
		return qxygnsDao.updateAppsysQxygns(appsysQxygns,gnbhs);
	}

	/**
	 * toDo:功能树初始化
	 * 
	 * @param ymbhs
	 * @param path
	 */
	@SuppressWarnings("unchecked")
	public String initGns(String ymbhs, String path) {
		String initSql = qxygnsDao.initGns(ymbhs);
		String fileName = "initQxygns.sql";
		try {
			File pathFile = new File(path);
			if (!pathFile.exists()) {
				pathFile.mkdir();
			}
			File file = new File(path + "\\" + fileName);
			if (file.exists()) {
				file.delete();
			} else {
				file.createNewFile();
			}
			FileWriter fw = new FileWriter(file, true);
			fw.write(initSql);
			fw.flush();
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			fileName = "";
		}
		return fileName;

	}

	/**
	 * toDo:权限与功能树排序;
	 * 
	 * @param ymbh
	 * @param pxh
	 * @return flag;
	 */
	public boolean orderQxygns(String[] ymbh, String[] pxh) {
		return qxygnsDao.orderQxygns(ymbh, pxh);
	}

	/**
	 * @toDo:得到与模块结合的功能List;
	 * @param ymbh
	 * @param type
	 * @return list;
	 */
	@SuppressWarnings("unchecked")
	public List<AppsysYmgnBean> findAppsysYmgnBean(String ymbh,int type) {
		return ymgnDao.findAppsysYmgnBean(null, ymbh, type);
	}

	/**
	 * @toDo:get权限与功能树Dao ;
	 * @return the qxygnsDao
	 */
	public QxygnsDao getQxygnsDao() {
		return qxygnsDao;
	}

	/**
	 * @toDo:set权限与功能树Dao ;
	 * @param qxygnsDao
	 *            the qxygnsDao to set
	 */
	public void setQxygnsDao(QxygnsDao qxygnsDao) {
		this.qxygnsDao = qxygnsDao;
	}

	/**
	 * @toDo:setSql语句;
	 * @param sjbh
	 * @param qxygns
	 */
	public void setSql(String sjbh, AppsysQxygns qxygns) {
		String ymmc = "";// 页面名称
		if (qxygns != null) {
			ymmc = qxygns.getYmmc() + "";
		}
		qxygnsDao.setSql(sjbh, ymmc);
	}

	/**
	 * @return the sql
	 */
	public String getSql() {
		return sql;
	}

	/**
	 * toDo:get
	 * 
	 * @return ymgnDao
	 */
	public YmgnDao getYmgnDao() {
		return ymgnDao;
	}

	/**
	 * toDo:set
	 * 
	 * @param ymgnDao
	 */
	public void setYmgnDao(YmgnDao ymgnDao) {
		this.ymgnDao = ymgnDao;
	}
}
